/*!
 * jQuery Validation Plugin v1.17.0
 *
 * https://jqueryvalidation.org/
 *
 * zhangyang 2018.5.15
 */
(function( factory ) {
	if ( typeof define === "function" && define.amd ) {
		define( ["jquery", "./jquery.validate"], factory );
	} else if (typeof module === "object" && module.exports) {
		module.exports = factory( require( "jquery" ) );
	} else {
		factory( jQuery );
	}
}(function( $ ) {

/** 字母数字或下划线*/
$.validator.addMethod("abc",function(value, element) {
	return this.optional(element) || /^[a-zA-Z0-9_]*$/.test(value);
},"请输入字母数字或下划线.");

/** 不相等 */
$.validator.addMethod( "notEqualTo", function( value, element, param ) {
	return this.optional( element ) || !$.validator.methods.equalTo.call( this, value, element, param );
}, "请再次输入不同的值." );

/**邮政编码验证 */
$.validator.addMethod("zipCode", function(value, element) {
    var tel = /^[0-9]{6}$/;
    return this.optional(element) || (tel.test(value));
}, "请输入正确的邮政编码");

/**手机号码 */
$.validator.addMethod("mobile", function(value, element) {
    var length = value.length;
    return this.optional(element) || (length == 11 && /^(\+?0?86\-?)?1[3456789]\d{9}$/.test(value));
}, "请输入正确的手机号码");

/**中文 */
$.validator.addMethod('CN', function(value, element, param){
    return this.optional(element) || /^([\u4E00-\u9FA5]|[\uFE30-\uFFA0]){0,}$/gi.test(value);
}, "请输入中文");

/**请输入小写字母 */
$.validator.addMethod('lowerCase', function(value, element){
	var isString = typeof value === 'string' || value instanceof String;
    return this.optional(element) || (isString && value === value.toLowerCase()) ;
}, "请输入小写字母");

/**请输入大写字母 */
$.validator.addMethod('upperCase', function(value, element){
	var isString = typeof value === 'string' || value instanceof String;
    return this.optional(element) || (isString && value === value.toUpperCase()) ;
}, "请输入大写字母");


/**JSON */
( function() {

	function isJSON( str ) {
		try {
			var obj = JSON.parse(str);
			return !!obj && (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) === 'object';
		  } catch (e) {/* ignore */}
		  return false;
	}
	$.validator.addMethod('isJSON', function(value, element){
		return this.optional(element) || isJSON(value);
	}, "请输入JSON格式字符串");

}() );

/**IP地址 包括IPv4 和 IPv6 */
( function() {

	var ipv4Maybe = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipv6Block = /^[0-9A-F]{1,4}$/i;
	function isIP(str) {
		var version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
		version = String(version);
		if (!version) {
		  return isIP(str, 4) || isIP(str, 6);
		} else if (version === '4') {
		  if (!ipv4Maybe.test(str)) {
			return false;
		  }
		  var parts = str.split('.').sort(function (a, b) {
			return a - b;
		  });
		  return parts[3] <= 255;
		} else if (version === '6') {
		  var blocks = str.split(':');
		  var foundOmissionBlock = false; // marker to indicate ::
	  
		  // At least some OS accept the last 32 bits of an IPv6 address
		  // (i.e. 2 of the blocks) in IPv4 notation, and RFC 3493 says
		  // that '::ffff:a.b.c.d' is valid for IPv4-mapped IPv6 addresses,
		  // and '::a.b.c.d' is deprecated, but also valid.
		  var foundIPv4TransitionBlock = isIP(blocks[blocks.length - 1], 4);
		  var expectedNumberOfBlocks = foundIPv4TransitionBlock ? 7 : 8;
	  
		  if (blocks.length > expectedNumberOfBlocks) {
			return false;
		  }
		  // initial or final ::
		  if (str === '::') {
			return true;
		  } else if (str.substr(0, 2) === '::') {
			blocks.shift();
			blocks.shift();
			foundOmissionBlock = true;
		  } else if (str.substr(str.length - 2) === '::') {
			blocks.pop();
			blocks.pop();
			foundOmissionBlock = true;
		  }
	  
		  for (var i = 0; i < blocks.length; ++i) {
			// test for a :: which can not be at the string start/end
			// since those cases have been handled above
			if (blocks[i] === '' && i > 0 && i < blocks.length - 1) {
			  if (foundOmissionBlock) {
				return false; // multiple :: in address
			  }
			  foundOmissionBlock = true;
			} else if (foundIPv4TransitionBlock && i === blocks.length - 1) {
			  // it has been checked before that the last
			  // block is a valid IPv4 address
			} else if (!ipv6Block.test(blocks[i])) {
			  return false;
			}
		  }
		  if (foundOmissionBlock) {
			return blocks.length >= 1;
		  }
		  return blocks.length === expectedNumberOfBlocks;
		}
		return false;
	  }

	$.validator.addMethod('isIP', function(value, element){
		return this.optional(element) || isIP(value);
	}, "请输入正确的IP地址");

}() );

/**身份证号码 */
( function() {
	
	var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 ];// 加权因子;
	var ValideCode = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ];// 身份证验证位值，10代表X;
	
	function isTrueValidateCodeBy18IdCard(a_idCard) {   
		var sum = 0; // 声明加权求和变量   
		if (a_idCard[17].toLowerCase() == 'x') {   
			a_idCard[17] = 10;// 将最后位为x的验证码替换为10方便后续操作   
		}   
		for ( var i = 0; i < 17; i++) {   
			sum += Wi[i] * a_idCard[i];// 加权求和   
		}   
		valCodePosition = sum % 11;// 得到验证码所位置   
		if (a_idCard[17] == ValideCode[valCodePosition]) {   
			return true;   
		}
		return false;   
	}
	
	function isValidityBrithBy18IdCard(idCard18){   
		var year = idCard18.substring(6,10);   
		var month = idCard18.substring(10,12);   
		var day = idCard18.substring(12,14);   
		var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
		// 这里用getFullYear()获取年份，避免千年虫问题   
		if(temp_date.getFullYear()!=parseFloat(year) || temp_date.getMonth()!=parseFloat(month)-1 || temp_date.getDate()!=parseFloat(day)){   
			return false;   
		}
		return true;   
	}
	
	function isValidityBrithBy15IdCard(idCard15){   
		var year =  idCard15.substring(6,8);   
		var month = idCard15.substring(8,10);   
		var day = idCard15.substring(10,12);
		var temp_date = new Date(year,parseFloat(month)-1,parseFloat(day));   
		// 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法   
		if(temp_date.getYear()!=parseFloat(year) || temp_date.getMonth()!=parseFloat(month)-1 || temp_date.getDate()!=parseFloat(day)){   
			return false;   
		}
		return true;
	}

	function isIdCard(idCard){
		if (idCard.length == 15) {   
			return isValidityBrithBy15IdCard(idCard);   
		}else if (idCard.length == 18){   
			var a_idCard = idCard.split("");// 得到身份证数组   
			if (isValidityBrithBy18IdCard(idCard)&&isTrueValidateCodeBy18IdCard(a_idCard)) {   
				return true;   
			}   
			return false;
		}
		return false;
	}

	$.validator.addMethod('IdCard', function(value, element){
		return this.optional(element) || isIdCard(value);
	}, "请输入正确的身份证号码");

}() );

}));